const cloud = require('wx-server-sdk');

cloud.init({
  env: cloud.DYNAMIC_CURRENT_ENV,
});

const db = cloud.database();
const _ = db.command;
const COLLECTION = 'account';

// 创建集合云函数入口函数
exports.main = async (event, context) => {
  const wxContext = cloud.getWXContext();
  const openId = wxContext.OPENID;
  const { userName } = event;
  const { userInfo } = event;
  await db.collection(COLLECTION).doc(userName)
    .update({
      data: {
        _openid: openId,
        ...userInfo,
        cnt: _.inc(1),
      },
    });
};
